// pages/paint/paint.js
Page({
  data:{
      color:'yellow',
      pen:3
  },
  onLoad:function(options){
      this.ctx = wx.createCanvasContext("myCanvas");
      var info = wx.getSystemInfoSync();
      this.setData({width:info.windowWidth,height:info.windowHeight});
  },
  onStartTap:function(event){
      console.log(event)
      //触摸点
      var startX = event.touches[0].x;
      var startY = event.touches[0].y;
      this.ctx.setStrokeStyle(this.data.color);
      this.ctx.setLineWidth(this.data.pen);
      this.ctx.setLineCap('round');
      this.ctx.beginPath();
      this.setData({startX:startX,startY:startY});
  },
  onMoveTap:function(event){
      var nStartX = event.touches[0].x;
      var nStartY = event.touches[0].y;
      this.ctx.moveTo(this.data.startX, this.data.startY);
      this.ctx.lineTo(nStartX, nStartY);
      this.ctx.stroke();
      this.setData({startX:nStartX,startY:nStartY});
      wx.drawCanvas({
         canvasId: 'myCanvas',
         reserve: true,
         actions: this.ctx.getActions() // 获取绘图动作数组
      })
  },
  onClearTap:function(event){
      this.ctx.clearRect(0,0,this.data.width,this.data.height);
      this.ctx.draw();
  },
  onColorTap:function(event){
      this.setData({color:event.currentTarget.dataset.item});
  },
  onPaintTap:function(event){
      this.setData({pen:event.currentTarget.dataset.item});
  },
  onSaveTap:function(){
      wx.canvasToTempFilePath({
        canvasId: 'myCanvas',
        success: function(res){
            var tempFile = res.tempFilePath;
            wx.saveFile({
              tempFilePath: tempFile,
              success: function(res){
                  console.log(res)  
              }
            })
        }
      })
      
  }
})